<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="hAdd">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用">
          <template slot-scope="{row}">
            <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
            <span v-else>{{ row.state === 1 ? '以启用' : '未启用' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
        >
          <template slot-scope="{row}">
            <template v-if="row.isEdit">
              <el-button type="text" size="small" @click="cOk(row)">确认</el-button>
              <el-button type="text" size="small" @click="row.isEdit = false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="small" @click="assign(row.id)">分配权限</el-button>
              <el-button type="text" size="small" @click="hEdit(row)">编辑</el-button>
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容确定删除吗？"
                @onConfirm="cDel(row.id)"
              >
                <el-button slot="reference" type="text" size="small">删除</el-button>
              </el-popconfirm>
            </template>

          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination background layout="sizes, prev, pager, next" :total="total" :pagesize="pagesize" :current-page="page" @current-change="change" @size-change="handleSizeChange" />
      </el-row>
    </div>

    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog">
      <!-- 表单内容 -->
      <el-form ref="roleForm" label-width="120px" :rules="rules" :model="roleForm">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="roleForm.state" size="mini" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="add">确定</el-button>
              <el-button size="mini">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>

    <assignP ref="refper" :c-id="cId" :show-dialog-p.sync="showDialogP" />
  </div>
</template>
<script>
import { addRole, delRole, getRole, putRole } from '@/api/role'
import assignP from './components/assignP.vue'
export default {
  name: 'Role',
  components: {
    assignP
  },
  data() {
    return {
      cId: -1,
      showDialogP: false,
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      showDialog: false,
      list: [],
      total: 0,
      pagesize: 10,
      page: 1,
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.get()
  },
  methods: {
    assign(id) {
      this.cId = id
      this.showDialogP = true
      this.$nextTick(() => {
        this.$refs.refper.get()
      })
    },
    async cDel(id) {
      await delRole(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) {
        if (this.page > 1) {
          this.page--
        }
      }
      this.get()
    },
    async cOk(row) {
      if (!row.editRow.name || !row.editRow.description) {
        this.$message.error('数据必填')
        return
      }
      await putRole(row.editRow)
      this.$message.success('更新成功')
      // eslint-disable-next-line require-atomic-updates
      row.isEdit = false
      this.get()
    },
    hEdit(row) {
      row.isEdit = true
    },
    async addC() {
      this.$refs.roleForm.validate()
      await addRole(this.roleForm)
      this.$message.success('添加成功')
      this.showDialog = false
      this.$refs.roleForm.resetFields()
      this.total += 1
      this.page = this.total / this.pagesize
      this.get()
    },
    add() {
      this.addC()
    },
    hAdd() {
      this.showDialog = true
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.get()
    },
    change(page) {
      this.page = page
      this.get()
    },
    async get() {
      const res = await getRole({ page: this.page, pagesize: this.pagesize })
      //
      this.list = res.data.rows
      this.total = res.data.total
      // 如果不加$set数据就不是响应式
      this.list.forEach(it => {
        this.$set(it, 'isEdit', false)
        this.$set(it, 'editRow', {
          id: it.id,
          name: it.name,
          state: it.state,
          description: it.description
        })
      })
    }
  }

}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
